eval 'exec perl $0 ${1+"$@"}' # -*- perl -*-
if 0;

# BETA RELEASE 4

# Copyright (c) 2005, Shawn Boyette
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
#  * Neither the name of the Olive Newsreader  nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

use warnings;
use strict;
use lib ".";

use Config::YAML;
use Curses::UI;
use DBI;
use XML::Simple;

use OliveEdit;
use OliveFeed;
use OliveOpts;
use OliveMisc;
use OliveStory;
use OliveWindow;

$XML::Simple::PREFERRED_PARSER = "XML::Parser";

my $rev = '$Rev$';
$rev =~ s/\D//g;
$rev = uc(sprintf("%4.4x",$rev));
my $tz  = `date +%z`;
$tz =~ s/00$//;

# create Curses::UI object
my $cui = new Curses::UI( -color_support => 1,
                          -clear_on_exit => 1,
                          -mouse_support => 0,
                        );

# system init; create windows and userdata storage
sysinit($cui);
my $root = $cui->add('root', 'Window');
my $ud = { wins  => wininit($cui,$root),
           c     => Config::YAML->new( config => "$ENV{HOME}/.olive/olive.yaml" ),
           dbh   => DBI->connect("dbi:SQLite:dbname=$ENV{HOME}/.olive/stories.db","",""),
           xml   => XML::Simple->new,
           feeds => "$ENV{HOME}/.olive/feeds",
           focus => 'news',
           rev   => $rev,
           opts  => [ qw( sls snu fos coe ) ],
           tz    => $tz,
         };
$cui->userdata($ud);

# set up database and keybindings
sysinit2($cui);
$cui->set_binding(sub { getfeed($cui) },         "\cA");
$cui->set_binding(sub { deletefeed($cui) },      "\cR");
$cui->set_binding(sub { editfeed($cui) },        "\cE");
$cui->set_binding(sub { setopts($cui) },         "\cO");
$cui->set_binding(sub { winredraw($cui,$root) }, "\cL");
$cui->set_binding(sub { aboutpop($cui) },        "\cT");
$root->set_binding(sub { followlink($cui) }, 'l');
$root->set_binding(sub { markone($cui,1) },  'm');
$root->set_binding(sub { markone($cui,0) },  'M');
$root->set_binding(sub { markall($cui,1) },  ',');
$root->set_binding(sub { markall($cui,0) },  '<');
$root->set_binding(sub { winflip($cui) },    'w');
$root->set_binding(sub { feedpoll($cui) },   'r');
$root->set_binding(sub { forcefetch($cui) }, 'R');
$root->set_binding(sub { quitit($cui) },     'Q');
$root->set_binding(sub { storyprev($cui) },  '[');
$root->set_binding(sub { storynext($cui) },  ']');
$root->set_binding(sub { helppop($cui) },    'h','?');

# go!
setpollwait($cui);
feedpoll($cui);
$cui->mainloop();

### End
